<template>
  <div class="login">
    <h2>欢迎您，请登录</h2>
    <van-form @submit="onSubmit">
      <van-field
        class="txt1"
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[
          {
            required: true,
            pattern: /^1[3-9]\d{9}$/,
            message: '请填写11位手机号',
          },
        ]"
      />
      <van-field
        class="txt1"
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, pattern: /^\d{4}$/, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          :disabled="disabled"
          class="btn"
          color="#f66"
          round
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link to="/register"
      ><div class="tips">没有账号? 去注册</div></router-link
    >
    <p>
      <van-checkbox
        class="icon"
        v-model="checked"
        icon-size="13px"
        @click="myclick()"
      ></van-checkbox>
      <span class="agreement"
        >已阅读并同意《嗨购平台服务协议隐私权政策》相关协议</span
      >
    </p>
  </div>
</template>

<script>
import { fun_login } from "../api/index.js";
import Vue from "vue";
import { Form } from "vant";
import { Field } from "vant";
import { Button } from "vant";
import { Toast } from "vant";
import { Checkbox, CheckboxGroup } from "vant";

Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Button);
Vue.use(Form);
Vue.use(Field);
Vue.use(Toast);
export default {
  data() {
    return {
      username: "",
      password: "",
      checked: false,
      disabled: true,
    };
  },
  methods: {
     myclick() {
      if (this.checked == true) {
        this.disabled = !this.disabled;
      } else {
         this.disabled=true
      }
    },
    onSubmit(values) {
      console.log("submit", values);
      fun_login(this.username, this.password).then((res) => {
        console.log(res);
        
        if (res.data.code == 200) {
          // 保存token
          localStorage.setItem("token", res.data.data.token);
          console.log(res.data.data.token);
          //   保存userid
          localStorage.setItem("userid", res.data.data.userid);
          Toast("登陆成功");
          this.$router.push("/homepage");
        }
      });
    },
   
  },
};
</script>

<style scoped>
.login {
  padding: 5.5556rem 1.1111rem;
}
h2 {
  margin-bottom: 2.7778rem;
  text-align: center;
}
.txt1 {
  margin-bottom: 1.1111rem;
  border-radius: 2.7778rem;
}
.tips {
  float: right;
  font-size: 0.6667rem;
}
p {
  display: flex;
  margin-top: 13.8889rem;
  align-items: center;
}
.agreement {
  font-size: 0.6667rem;
  display: inline-block;
  margin-left: 0.1667rem;
}
</style>